<template>
  <div class="login-container">
    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 头部标题 -->
      <div class="login-header">
        <div class="logo">🔐</div>
        <h2>Flask + JWT 认证系统</h2>
        <p class="subtitle">欢迎回来，请登录您的账户</p>
      </div>

      <!-- 登录表单 -->
      <div class="login-form">
        <!-- 用户名输入框 -->
        <div class="form-group">
          <label for="username">
            <span class="icon">👤</span>
            用户名
          </label>
          <input
            id="username"
            v-model="loginForm.username"
            type="text"
            placeholder="请输入用户名 (admin)"
            @keyup.enter="handleLogin"
            :disabled="loading"
          />
        </div>

        <!-- 密码输入框 -->
        <div class="form-group">
          <label for="password">
            <span class="icon">🔒</span>
            密码
          </label>
          <input
            id="password"
            v-model="loginForm.password"
            :type="showPassword ? 'text' : 'password'"
            placeholder="请输入密码 (123456)"
            @keyup.enter="handleLogin"
            :disabled="loading"
          />
          <span class="toggle-password" @click="showPassword = !showPassword">
            {{ showPassword ? '👁️' : '👁️‍🗨️' }}
          </span>
        </div>

        <!-- 提示信息 -->
        <div v-if="message" :class="['message', messageType]">
          {{ message }}
        </div>

        <!-- 登录按钮 -->
        <button 
          class="login-button" 
          @click="handleLogin"
          :disabled="loading"
        >
          <span v-if="!loading">🚀 登录</span>
          <span v-else class="loading-spinner">⏳ 登录中...</span>
        </button>

        <!-- 提示文字 -->
        <div class="tips">
          <p>💡 测试账号：admin / 123456</p>
          <p>🔧 技术栈：Flask + Vue3 + JWT + Redis</p>
        </div>
      </div>
    </div>

    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 状态控制
const loading = ref(false)
const showPassword = ref(false)
const message = ref('')
const messageType = ref('info') // info, success, error

// 登录处理函数
const handleLogin = async () => {
  // 验证输入
  if (!loginForm.username.trim() || !loginForm.password.trim()) {
    showMessage('请输入用户名和密码', 'error')
    return
  }

  loading.value = true
  message.value = ''

  try {
    // 发送登录请求
    const response = await axios.post('http://localhost:5000/api/user/login', {
      user: loginForm.username,  // 后端使用user字段
      password: loginForm.password
    })

    if (response.data.code === 200) {
      // 登录成功
      showMessage('登录成功！正在跳转...', 'success')

      // 保存token和用户信息到localStorage
      const { access_token, refresh_token, user } = response.data.data
      localStorage.setItem('access_token', access_token)
      localStorage.setItem('refresh_token', refresh_token)
      localStorage.setItem('username', user.user)
      localStorage.setItem('user_role', user.role)
      localStorage.setItem('user_id', user.id)

      // 延迟跳转，让用户看到成功提示
      setTimeout(() => {
        router.push('/home')
      }, 1000)
    } else {
      showMessage(response.data.message || '登录失败', 'error')
    }
  } catch (error) {
    console.error('登录错误:', error)
    
    if (error.response) {
      // 服务器返回错误
      showMessage(error.response.data.message || '登录失败，请检查用户名和密码', 'error')
    } else if (error.request) {
      // 网络错误
      showMessage('网络错误，请检查后端服务是否启动（http://localhost:5000）', 'error')
    } else {
      showMessage('登录失败：' + error.message, 'error')
    }
  } finally {
    loading.value = false
  }
}

// 显示消息
const showMessage = (msg, type = 'info') => {
  message.value = msg
  messageType.value = type
}
</script>

<style scoped>
/* 全局容器 */
.login-container {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  overflow: hidden;
}

/* 背景装饰圆圈 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 20s infinite ease-in-out;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -50px;
  right: -50px;
  animation-delay: 5s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  top: 50%;
  right: 10%;
  animation-delay: 10s;
}

@keyframes float {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-30px) scale(1.1); }
}

/* 登录卡片 */
.login-card {
  position: relative;
  z-index: 1;
  width: 420px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  padding: 40px;
  animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  font-size: 60px;
  margin-bottom: 10px;
  animation: bounce 2s infinite;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.login-header h2 {
  font-size: 26px;
  color: #333;
  margin: 10px 0;
  font-weight: 700;
}

.subtitle {
  font-size: 14px;
  color: #666;
  margin: 5px 0 0 0;
}

/* 表单 */
.login-form {
  margin-top: 20px;
}

.form-group {
  position: relative;
  margin-bottom: 20px;
}

.form-group label {
  display: flex;
  align-items: center;
  font-size: 14px;
  font-weight: 600;
  color: #555;
  margin-bottom: 8px;
}

.form-group label .icon {
  margin-right: 6px;
  font-size: 16px;
}

.form-group input {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  font-size: 15px;
  transition: all 0.3s;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

.form-group input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.toggle-password {
  position: absolute;
  right: 16px;
  top: 43px;
  cursor: pointer;
  font-size: 18px;
  user-select: none;
}

/* 消息提示 */
.message {
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 14px;
  animation: fadeIn 0.3s;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.success {
  background-color: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.message.error {
  background-color: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.message.info {
  background-color: #d1ecf1;
  color: #0c5460;
  border: 1px solid #bee5eb;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.login-button:active:not(:disabled) {
  transform: translateY(0);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.loading-spinner {
  display: inline-block;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 提示文字 */
.tips {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid #e0e0e0;
  text-align: center;
}

.tips p {
  font-size: 13px;
  color: #888;
  margin: 8px 0;
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    width: 90%;
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 22px;
  }
  
  .logo {
    font-size: 50px;
  }
}
</style>
